from .internal_page import InternalPage
from .blocks.film_form import FilmForm
from selenium.webdriver.common.by import By
#from selenium.webdriver.support.select import Select


class AddFilmPage(InternalPage):

    def __init__(self, driver, base_url):
        super(AddFilmPage, self).__init__(driver, base_url)
        self.film_form = FilmForm(self.driver, self.base_url)

    @property
    def logout_button(self):
        return self.driver.find_element_by_css_selector(
            "nav a[href $= '?logout']")

    @property
    def user_profile_link(self):
        return self.driver.find_element_by_css_selector(
            "nav a[href $= '?go=profile']")

    @property
    def user_management_link(self):
        return self.driver.find_element_by_css_selector(
            "nav a[href $= '?go=users']")

    @property
    def is_this_page(self):
        return self.is_element_visible(
            (By.CSS_SELECTOR, "input[type=\"submit\"]"))

    @property
    def IMDB_search_editbox(self):
        return self.driver.find_element_by_id("imdbsearch")

    @property
    def IMDB_submit_button(self):
        return self.driver.find_element_by_css_selector(
            "input[type=\"submit\"]")

    def FillAllFieldsWith(self, message):
        table = self.film_form.main_table
        for element in table.find_elements_by_xpath("//textarea"):
            was = element.text
            if len(was) == 0:
                element.send_keys(message)
            else:
                element.send_keys(was + "\n" + message)

